﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Collections.Generic;

public partial class DayToDayManagement_ProductUpStatistics : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }

    private string Condition
    {
        get
        {
            string condition = "";
            if (ViewState["Condition"] != null)
            {
                condition = ViewState["Condition"].ToString();
            }
            return condition;
        }
        set
        {
            ViewState["Condition"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("300100102");
        if (!IsPostBack)
        {
            AspNetPager1.PageSize = Convert.ToInt32(ConfigurationManager.AppSettings["RowCountPerPage"].ToString());
            BindDropUpOperator();
            BindDropAuditor();
            //BindRepeater("");
        }
    }

    #region 绑定控件
    private void BindDropUpOperator()
    {
        List<Model.USERS> users = new BLL.USERS().GetAllList();
        dropUpOperator.DataSource = users;
        dropUpOperator.DataTextField = "USERS_NAME";
        dropUpOperator.DataValueField = "USERS_CODE";
        dropUpOperator.DataBind();
        dropUpOperator.Items.Insert(0, "");
    }

    private void BindDropAuditor()
    {
        List<Model.USERS> users = new BLL.USERS().GetAllList();
        dropAuditor.DataSource = users;
        dropAuditor.DataTextField = "USERS_NAME";
        dropAuditor.DataValueField = "USERS_CODE";
        dropAuditor.DataBind();
        dropAuditor.Items.Insert(0, "");
    }

    private void BindRepeater(string sqlCondition)
    {
        int rowCount;
        DataSet dst = new BLL.CMDUP().GettProductUpStatistics(GetStartIndex(AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize), AspNetPager1.PageSize, sqlCondition, out rowCount);
        rptUpStatistics.DataSource = dst;
        rptUpStatistics.DataBind();
        AspNetPager1.RecordCount = rowCount;
    }
    #endregion
    
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        bool isFirstCondition = true;
        StringBuilder sqlCondition = new StringBuilder();
        sqlCondition.Append("where ");
        if (hiddenUpTypes.Value != "")
        {
            sqlCondition.Append("UT_CODE in (");
            sqlCondition.Append(hiddenUpTypes.Value);
            sqlCondition.Append(") ");
            isFirstCondition = false;
        }
        if (hiddenProduct.Value != "")
        {
            if (isFirstCondition)
            {
                sqlCondition.Append("SHANGPIN.SPDM in (");
                sqlCondition.Append(hiddenProduct.Value);
                sqlCondition.Append(") ");
                isFirstCondition = false;
            }
            else
            {
                sqlCondition.Append("and SHANGPIN.SPDM in (");
                sqlCondition.Append(hiddenProduct.Value);
                sqlCondition.Append(") ");
            }
        }
        if (dropUpOperator.SelectedIndex != 0)
        {
            if (isFirstCondition)
            {
                sqlCondition.Append("CU_UPER ='");
                sqlCondition.Append(dropUpOperator.SelectedItem.Text);
                sqlCondition.Append("' ");
                isFirstCondition = false;
            }
            else
            {
                sqlCondition.Append("and CU_UPER ='");
                sqlCondition.Append(dropUpOperator.SelectedItem.Text);
                sqlCondition.Append("' ");
            }
        }
        if (dropAudit.SelectedIndex != 0)
        {
            if (isFirstCondition)
            {
                sqlCondition.Append("CU_CHECK =");
                sqlCondition.Append(dropAudit.SelectedItem.Value);
                sqlCondition.Append(" ");
                isFirstCondition = false;
            }
            else
            {
                sqlCondition.Append("and CU_CHECK =");
                sqlCondition.Append(dropAudit.SelectedItem.Value);
                sqlCondition.Append(" ");
            }
        }
        if (dropAuditor.SelectedIndex != 0)
        {
            if (isFirstCondition)
            {
                sqlCondition.Append("CU_CHECKER='");
                sqlCondition.Append(dropAuditor.SelectedItem.Value);
                sqlCondition.Append("' ");
                isFirstCondition = false;
            }
            else
            {
                sqlCondition.Append("and CU_CHECKER='");
                sqlCondition.Append(dropAuditor.SelectedItem.Value);
                sqlCondition.Append("' ");
            }
        }
        try
        {
           
            if (txtUpStartTime.Text != "" && txtUpEndTime.Text != "")
            {
                if (isFirstCondition)
                {
                    sqlCondition.Append("CU_UPDATE>'");
                    sqlCondition.Append(DateTime.Parse(txtUpStartTime.Text));
                    sqlCondition.Append("' and CU_UPDATE<'");
                    sqlCondition.Append(DateTime.Parse(txtUpEndTime.Text));
                    sqlCondition.Append("' ");
                    isFirstCondition = false;
                }
                else
                {
                    sqlCondition.Append("and CU_UPDATE>'");
                    sqlCondition.Append(DateTime.Parse(txtUpStartTime.Text));
                    sqlCondition.Append("' and CU_UPDATE<'");
                    sqlCondition.Append(DateTime.Parse(txtUpEndTime.Text));
                    sqlCondition.Append("' ");
                }
            }
            if (!isFirstCondition)
            {
                this.Condition = sqlCondition.ToString();
            }
            else
            {
                this.Condition = "";
            }
            BindRepeater(this.Condition);
        }


        catch (FormatException EX)
        {
            Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('输入的日期格式有误，请重新输入。')</script>");
        }
        catch
        {
            Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('查询时出现错误，请重新查询。')</script>");
        }
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindRepeater(this.Condition);
    }

    /// <summary>
    /// 分页时计算记录起始下标
    /// </summary>
    /// <param name="pageIndex"></param>
    /// <param name="pageSize"></param>
    /// <returns></returns>
    private int GetStartIndex(int pageIndex, int pageSize)
    {
        return 1 + (pageIndex - 1) * pageSize;
    }
}
